import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/emergency_event/ui/emergency_event_detail_page.dart';
import 'package:ls_work/emergency_event/view_model/edit_emergency_event_view_model.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/widgets/event_input_widget.dart';

import '../../bad_event/ui/bad_event_list_page.dart';
import '../../net/api/http_api.dart';
import '../../res/colors.dart';
import '../../res/gaps.dart';
import '../../widgets/common_widget.dart';

class EditEmergencyEventPage extends BaseStatefulMvvmWidget {
  dynamic id;

  EditEmergencyEventPage({this.id});

  @override
  State<StatefulWidget> createState() => _EditEmergencyEventState();
}

class _EditEmergencyEventState
    extends BaseMvvmState<EditEmergencyEventViewModel, EditEmergencyEventPage> {
  Widget get arrowRight => Icon(
        Icons.keyboard_arrow_right,
        size: 15,
        color: Colours.gray_99,
      );

  @override
  Widget? buildLoadingContentView() {
    var detail = viewModel.detailData;
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(
            height: 0,
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TitleValueWidget(
                  title: '基本信息',
                  value: '',
                  showArrow: false,
                  hint: '',
                  head: Container(
                    width: 3,
                    height: 20,
                    color: Colors.blue,
                  )),
              Gaps.dividerLine,
              ...[
                TitleValueWidget(
                        title: '事件定性',
                        value: detail?.eventNatureName ?? "",
                        hint: '请选择',
                        isRequired: false,
                        contentWeight: -1,
                        arrow: arrowRight)
                    .onTap(() {
                  viewModel.chooseEventNature();
                }),
                TitleValueWidget(
                        title: '事件类型',
                        value: detail?.eventTypeName ?? "",
                        hint: '请选择',
                        isRequired: false,
                        contentWeight: -1,
                        arrow: arrowRight)
                    .onTap(() {
                  viewModel.chooseEventType();
                }),
                TitleValueWidget(
                        title: '发生时间',
                        value: (detail?.startDate ?? "")
                            .changeTime(format: [yyyy, '-', mm, '-', dd]),
                        hint: '请选择',
                        isRequired: false,
                        contentWeight: -1,
                        arrow: arrowRight)
                    .onTap(() {
                  viewModel.chooseStartTime();
                }),
                TitleValueWidget(
                        title: '相关人类型',
                        value: detail?.relatedTypeName ?? "",
                        hint: '请选择',
                        isRequired: false,
                        contentWeight: -1,
                        arrow: arrowRight)
                    .onTap(() {
                  viewModel.chooseComboInfo(
                      code: '10019',
                      currentValue: detail?.relatedType,
                      onApply: (item) {
                        setState(() {
                          viewModel.inEditMode = false;
                          detail?.relatedTypeName = item.nameCn;
                          detail?.relatedType = item.codeValue;
                        });
                        Future.delayed(Duration(milliseconds: 400), () {
                          setState(() {
                            viewModel.inEditMode = true;
                          });
                        });
                      });
                }),
                if (detail?.relatedTypeName != null) ...[
                  detail?.relatedTypeName == '长者' ||
                          detail?.relatedTypeName == '员工'
                      ? TitleValueWidget(
                              title: '相关人姓名',
                              value: detail?.name ?? "",
                              hint: '请选择',
                              isRequired: false,
                              contentWeight: -1,
                              arrow: arrowRight)
                          .onTap(() {
                          if (detail?.relatedTypeName == '长者') {
                            viewModel.chooseOld();
                          } else {
                            viewModel.chooseStaff();
                          }
                        })
                      : TitleInputWidget(
                          title: '相关人姓名',
                          onChanged: (str) {
                            detail?.name = str;
                          },
                          text: detail?.name ?? "",
                          isRow: true,
                          inEdit: viewModel.inEditMode,
                        ),
                  TitleInputWidget(
                    title: '联系方式',
                    hint: '请输入',
                    isRequired: false,
                    contentWeight: 1,
                    inEdit: viewModel.inEditMode,
                    text: detail?.contact ?? "",
                    isRow: true,
                    onChanged: (str) {
                      detail?.contact = str;
                    },
                    keyboardType: CommonInputType.phone,
                  ),
                  TitleInputWidget(
                    title: '房间号',
                    hint: '请输入',
                    text: detail?.roomNo ?? "",
                    onChanged: (str) {
                      detail?.roomNo = str;
                    },
                    isRequired: false,
                    contentWeight: 1,
                    isRow: true,
                    inEdit: viewModel.inEditMode,
                  ),
                ],
                TitleInputWidget(
                  title: '第一处理人',
                  text: detail?.firstHandler ?? "",
                  hint: '请输入',
                  isRequired: false,
                  contentWeight: 1,
                  isRow: true,
                  onChanged: (str) {
                    detail?.firstHandler = str;
                  },
                ),
                TitleInputWidget(
                  title: '发生地点',
                  text: detail?.place ?? "",
                  hint: '请输入',
                  isRow: true,
                  isRequired: false,
                  contentWeight: 1,
                  onChanged: (str) {
                    detail?.place = str;
                  },
                ),
              ].insertWidget(
                Gaps.dividerLine,
              )
            ],
          ).addToContainer(
              decoration: BoxDecoration(
                color: Colors.white,
              ),
              padding: EdgeInsets.symmetric(horizontal: 16)),
          Column(
            children: [
              TitleValueWidget(
                  title: '详细信息',
                  value: '',
                  hint: '',
                  showArrow: false,
                  head: Container(
                    width: 3,
                    height: 20,
                    color: Colors.blue,
                  )),
              TitleInputWidget(
                title: '事件经过',
                text: detail?.incident ?? "",
                onChanged: (str) {
                  detail?.incident = str;
                },
              ),
              TitleInputWidget(
                title: '处理措施',
                text: detail?.measures ?? "",
                onChanged: (str) {
                  detail?.measures = str;
                },
              ),
              TitleInputWidget(
                title: '产生后果',
                text: detail?.consequence ?? "",
                onChanged: (str) {
                  detail?.consequence = str;
                },
              ),
              TitleInputWidget(
                title: '事件总结',
                text: detail?.conclusion ?? "",
                onChanged: (str) {
                  detail?.conclusion = str;
                },
              ),
              TitleInputWidget(
                title: '备注',
                text: detail?.remark ?? "",
                onChanged: (str) {
                  detail?.remark = str;
                },
              ),
            ].insertWidget(Gaps.dividerLine),
          ).addToContainer(
              decoration: BoxDecoration(
                color: Colors.white,
              ),
              padding: EdgeInsets.symmetric(horizontal: 16)),
            Column(
              children: [
                TitleValueWidget(
                    title: '附件',
                    value: '',
                    hint: '',
                    arrow: Icon(Icons.add_box_outlined,color: Colors.blue,),
                    head: Container(
                      width: 3,
                      height: 20,
                      color: Colors.blue,
                    )).onTap((){
                      viewModel.chooseImage();
                }),
                ...(detail!.attachList??=[])
                    .map((e) {
                      return Row(
                        children: [
                          Image.asset(
                            e.fileType.isImage
                                ? getImagePath('image_image', parent: 'assets')
                                : getImagePath('image_file', parent: 'assets'),
                            width: 35,
                            height: 35,
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          CommonText(e.fileName).setWeight(1),
                          CommonText('删除', textColor: Colors.red).onTap((){
                            setState(() {
                              detail.attachList?.remove(e);
                            });
                          })
                        ],
                      ).onTap(() {
                        if (e.fileType.isImage) {
                          showBigImage(
                              context, ['${HttpApi.imageHead}${e.id}']);
                        } else {}
                      });
                    })
                    .toList()
                    .insertWidget(SizedBox(
                      height: 12,
                    )),
                SizedBox(
                  height: 12,
                )
              ],
            ).addToContainer(
              decoration: BoxDecoration(
                color: Colors.white,
              ),
              padding: EdgeInsets.symmetric(horizontal: 16),
            ),
          SizedBox(
            height: 10,
          ),
          Row(
            children: [
              SizedBox(
                width: 16,
              ),
              TextButton(
                onPressed: () {
                  viewModel.submitEvent(true);
                },
                child: CommonText('保存', textColor: Colors.white),
                style: btnStyle(Colors.orange),
              ).setWeight(1),
              SizedBox(
                width: 16,
              ),
              TextButton(
                onPressed: () {
                  viewModel.submitEvent(false);
                },
                child: CommonText('提交', textColor: Colors.white),
                style: btnStyle(Colors.blue),
              ).setWeight(1),
              SizedBox(
                width: 16,
              ),
            ],
          ),
          SizedBox(height: 10,)
        ].insertWidget(SizedBox(
          height: 12,
        )),
      ),
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text('紧急事件'),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  EditEmergencyEventViewModel createViewModel() {
    return EditEmergencyEventViewModel(context, id: widget.id);
  }

  @override
  void onRetryClick() {
    viewModel.initData();
  }

  ButtonStyle btnStyle(Color color) {
    return TextButton.styleFrom(
        backgroundColor: color,
        shape: RoundedRectangleBorder(borderRadius: 6.borderRadius));
  }
}
